package com.stevenxin.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.stevenxin.pojo.Users;
import com.stevenxin.service.IUserService;
import com.stevenxin.utils.JSONResult;
import com.stevenxin.utils.MD5Utils;
import com.stevenxin.vo.UsersVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(value="用户业务相关接口", tags= {"用户业务相关"})
public class UserController extends BasicController {
	
	@Autowired
	IUserService userService;
	
	@ApiOperation(value="用户上传头像", notes="用户上传头像")
	@ApiImplicitParam(name="userId", value="用户ID", required=true, dataType="String", paramType="query")
	@PostMapping("/uploadFace")
	public JSONResult uploadFace(String userId, @RequestParam("file") MultipartFile [] files) throws Exception {
		File testFile=new File("aa.txt");
		System.out.println(testFile.getAbsolutePath());
		String fileSpace="src/main/webapp";
		String uploadPathDB="/minivideo/"+userId+"/face";
		FileOutputStream fos=null;
		InputStream is=null;
		try {
			if(files!=null && files.length>0) {
				String fileName=files[0].getOriginalFilename();
				if(StringUtils.isNotBlank(fileName)) {
					String finalFacePath=fileSpace+uploadPathDB+"/"+fileName;
					uploadPathDB=uploadPathDB+"/"+fileName;
					File outFile=new File(finalFacePath);
					if(outFile.getParentFile().exists()==false) {
						outFile.getParentFile().mkdirs();
					}
					fos = new FileOutputStream(outFile);
					is=files[0].getInputStream();
					IOUtils.copy(is, fos);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(fos!=null) {
				fos.flush();
				fos.close();
			}
		}
		return JSONResult.ok(uploadPathDB);
	}

}
